<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" title="保养设置" width="650px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">保养下达单</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row>
          <el-form-item label="保养单号" prop="maintainNo">
            <el-input v-model="ruleForm.maintainNo" maxlength="20" show-word-limit />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="下达人" prop="releaseBy">
            <el-select v-model="ruleForm.releaseBy" style="width: 100%">
              <el-option v-for="item in userAllList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="下达时间" prop="releaseTime">
            <el-date-picker v-model="ruleForm.releaseTime" type="datetime" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" />
          </el-form-item>
        </el-row>
      </el-form>
    </el-row>
  </el-dialog>
</template>
<script>
import { timeFormatToString } from '@/utils/function'
export default {
  props: {
    userAllList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const checkMaintainNo = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/equip/maintain/task/verifyCodeUnique?maintainNo=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('保养单号重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      ruleForm: {
        maintainNo: '',
        releaseTime: timeFormatToString(new Date()).slice(0, 16),
        releaseBy: ''
      },
      rules: {
        releaseBy: [
          { required: true, message: '请选择下达人！', trigger: 'change' }
        ],
        releaseTime: [
          { required: true, message: '请选择下达时间！', trigger: 'change' }
        ],
        maintainNo: [
          { required: true, message: '保养单号不能为空！', trigger: 'blur' },
          { validator: checkMaintainNo, trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      detailInfo: {}
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showInfo(row) {
      this.ruleForm = { ...this.ruleForm, ...row }
      console.log(this.ruleForm)
      this.dialogVisible = true
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/equip/maintain/info/saveMaintainTask',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        maintainNo: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
